package com.ts.robot.socket;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.thunder.rebot.rebotclient.constant.Constants;
import com.thunder.rebot.rebotclient.entity.ContentMessageBean;
import com.thunder.rebot.rebotclient.entity.MessageBean;
import com.thunder.rebot.rebotclient.entity.QuerySQLEntity;
import com.thunder.rebot.rebotclient.entity.RealetimePositionBean;
import com.thunder.rebot.rebotclient.entity.ResponseMessageBean;
import com.thunder.rebot.rebotclient.service.ServiceClient;
import com.ts.robot.socket.lisetner.SocketLisetnerInterface;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class TSRobotSocketNative {

    public TSRobotSocketNative(){}
    private  static String TAG =  TSRobotSocketNative.class.getSimpleName();
//    private static TSRobotSocketNative tsRobotSocketNative = new TSRobotSocketNative(null);

    private static String FRAME = "frame";
    public static final String LOGIN = "1001";
//    private static String LOGIN = "1002";
public static final String MISSION = "1003";
    public static final String CONFIRM_ID = "1004";
    public static final String CONFIRM_OP = "1005";
    public static final String RE_LOGIN = "1006";
    public static final String INFORM = "1007";
    public static final String BROADCAST = "1008";
    public static final String OPERATIONCHECK = "1009";//任务描述回复
    public static final String SYSMSG = "1010";//系统消息
    public static final String SWITCH = "1011";
    public static final String RE_SWITCH="1012";//对切换的响应
    public static final String WIFILISTVOICE = "1013";


    /***
     * 响应码
     */
    private static int RE_ACCEPTED = 101;
    private static int RE_NO_ID = 102;
    private static int RE_WRONG_ID_PW = 103;
    private static int RE_UNKNOWN = 100;

//    public static TSRobotSocketNative getTsRobotSocketNativeInstance(){
//        return tsRobotSocketNative;
//    }
    private static ArrayList<SocketLisetnerInterface> socketLisetnerInterface = new ArrayList<>();

    public void addSocketLisetnerInterface (SocketLisetnerInterface socketLisetnerInterface1){
        if(socketLisetnerInterface.contains(socketLisetnerInterface1)){
            return;
        }
        socketLisetnerInterface.add(socketLisetnerInterface1);
        Log.i(TAG,"WP@@: listener size" + socketLisetnerInterface.size());
    }

    public void removeSocketLisetnerInterface(SocketLisetnerInterface socketLisetnerInterface1){
        if(socketLisetnerInterface1 != null){
            if(socketLisetnerInterface.contains(socketLisetnerInterface1)){
                socketLisetnerInterface.remove(socketLisetnerInterface1);
            }
        }
    }

    /**
     * 加载 native;
     */
    static {
        System.loadLibrary("robotsocket");
    }


private  static boolean isConfirm = false;
    private static SharedPreferences sharedPreferences;
    public TSRobotSocketNative(Context context){
        init_listener_native(this);//初始化回调
        sharedPreferences = context.getSharedPreferences("data", Context.MODE_PRIVATE);

    }
    public void init(String ip){

    //        init_native_socket("192.168.169.103");//初始化本地socket
        init_native_socket(ip);//初始化本地socket
    }

    public native String testString2Char(String str);//test

    public native void send_data_native(String sendData);//发送数据

//    public native void init_native_socket(String ip);//初始化 robotsocket
    public native void init_native_socket(String ip);//初始化 robotsocket
    public native void init_listener_native(Object object);//初始化 listener

    public native void quit_native(boolean flag);

    public boolean isConfirm() {
        return isConfirm;
    }

    public void setConfirm(boolean confirm) {
        isConfirm = confirm;
    }

    /*
        回调函数
         */
    private static void postEventFromNative(Object mediaplayer_ref, int what, String arg1, String arg2, Object obj) {
        Log.d(TAG,"revice what :" + what + "-arg1 :" + arg1 + " arg2 :" + arg2 + " -obj:" + obj);
        //传递json字符串   arg2
        switch (what ){
            case 10:
                if(socketLisetnerInterface != null && socketLisetnerInterface.size() > 0){
                    for (SocketLisetnerInterface listner : socketLisetnerInterface){
//                        listner.getLocation(realetimePositionBean.getX(),realetimePositionBean.getY(),realetimePositionBean.getZ(),realetimePositionBean.getW());
                        listner.conectState(1,arg1);
                    }
                }
                break;
            case 11:
                if(arg1 != null){
                    JSONObject objq = null;
                    String flag = null;
                    try {
                        objq = new JSONObject(arg1);
                        flag = objq.getString(FRAME);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if(flag != null){
                        switch (flag){
                            case LOGIN:
//                    LoginMessageBean loginMessageBean = JSON.parseObject(arg1, LoginMessageBean.class);
//                    loginMessageBean
//                    ResponseMessageBean responseMessageBean = JSON.parseObject(arg1, ResponseMessageBean.class);
//                    Log.d(TAG,"log respons :" + responseMessageBean.toString());
                                break;
                            case MISSION://1003

                                break;
                            case CONFIRM_ID://任务结束时 人脸确认
//                    JSON.parseObject(arg1, LoginMessageBean.class);

                                break;
                            case CONFIRM_OP://收到ros过来的信息

                                MessageBean messageBean = JSON.parseObject(arg1, MessageBean.class);

                                break;
                            case RE_LOGIN://登录的响应
                                ResponseMessageBean responseMessageBean = JSON.parseObject(arg1, ResponseMessageBean.class);
                                Log.d(TAG,"response :" + responseMessageBean.toString());
                                if(socketLisetnerInterface != null && socketLisetnerInterface.size() > 0){
                                    for (SocketLisetnerInterface listner : socketLisetnerInterface){
                                        listner.loginLisetner(Integer.valueOf(responseMessageBean.getRESPOND()));
                                    }
                                }
                                break;
                            case INFORM://收到平台给手机发的任务消息，并显示
                                isConfirm = true;
                                ContentMessageBean contentMessageBean = JSON.parseObject(arg1, ContentMessageBean.class);
                                if(socketLisetnerInterface != null && socketLisetnerInterface.size() > 0){
                                    for (SocketLisetnerInterface listner : socketLisetnerInterface){
                                        listner.operateConfirm(contentMessageBean);
                                    }
                                }
                                break;
                            case BROADCAST://实时信息
                                RealetimePositionBean realetimePositionBean = JSON.parseObject(arg1, RealetimePositionBean.class);
                                Log.d(TAG,"positon:" + realetimePositionBean.toString());
                                if(socketLisetnerInterface != null && socketLisetnerInterface.size() > 0){
                                    for (SocketLisetnerInterface listner : socketLisetnerInterface){
                                        listner.getLocation(realetimePositionBean.getX(),realetimePositionBean.getY(),realetimePositionBean.getZ(),realetimePositionBean.getW());
                                    }
                                }
                                break;
                            case OPERATIONCHECK://动作确认
                                ResponseMessageBean responseMessageBean1 = JSON.parseObject(arg1, ResponseMessageBean.class);
                                if(socketLisetnerInterface != null && socketLisetnerInterface.size() > 0){
                                    for (SocketLisetnerInterface listner : socketLisetnerInterface){
                                        listner.taskConfirm(responseMessageBean1);
                                    }
                                }
                                break;
                            case WIFILISTVOICE:
                                //要获取wifi list 和 噪声
                                //请求传感器的值
                                final QuerySQLEntity.ResponseQuery request = new QuerySQLEntity.ResponseQuery();
                                request.type = 100;
                                new Thread("send") {
                                    @Override
                                    public void run() {
                                        super.run();
                                        ServiceClient.client.sendTCP(request);
                                    }
                                }.start();
                                break;

                            case RE_SWITCH:
                                ResponseMessageBean responseMessageBean2 = JSON.parseObject(arg1, ResponseMessageBean.class);
                                if(socketLisetnerInterface != null && socketLisetnerInterface.size() > 0){
                                    for (SocketLisetnerInterface listner : socketLisetnerInterface){
                                        listner.changeModeListener(responseMessageBean2);
                                    }
                                }
                                break;
                        }
                    }else{
                        Log.e(TAG,"could not find frame");
                    }

                }
                break;
        }




    }




    /*
查询数据库，查询采集到的人脸是否是目标人物
 */
    private static void queryRequest(String name){
        if(!TextUtils.isEmpty(name)){
            final QuerySQLEntity.RequestQuery requestQuery = new QuerySQLEntity.RequestQuery();
            requestQuery.type = Constants.QUERYCONFIRMCODE;
            requestQuery.name = name;
            new Thread("send") {
                @Override
                public void run() {
                    super.run();
                    ServiceClient.client.sendTCP(requestQuery);
                }
            }.start();

        }

    }

}
